/**    
 * @Title: WbAutoConfiguration.java  
 * @Package cn.hh.wb.configuration  
 * @Description: TODO(用一句话描述该文件做什么)  
 * @author 348953327@qq.com    
 * @date May 23, 2020 10:38:36 PM  
 * @version V1.0    
 */
package cn.hh.wb.configuration;

import java.io.File;
import java.io.FileNotFoundException;

import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.ContextResource;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.jndi.JndiObjectFactoryBean;

import com.alibaba.druid.pool.DruidDataSource;

import cn.hh.wb.configuration.properties.DruidStatProperties;
import cn.hh.wb.configuration.stat.DruidFilterConfiguration;
import cn.hh.wb.configuration.stat.DruidSpringAopConfiguration;
import cn.hh.wb.configuration.stat.DruidStatViewServletConfiguration;
import cn.hh.wb.configuration.stat.DruidWebStatFilterConfiguration;
import cn.hh.wb.configuration.stat.WbWebStatFilterConfiguration;

/**  
 * @ClassName: WbAutoConfiguration  
 * @Description: TODO(这里用一句话描述这个类的作用)  
 * @author 348953327@qq.com  
 * @date May 23, 2020 10:38:36 PM  
 *    
 */
@Configuration
@ConditionalOnClass(DruidDataSource.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties({DruidStatProperties.class, DataSourceProperties.class})
@Import({DruidSpringAopConfiguration.class,
    DruidStatViewServletConfiguration.class,
    DruidWebStatFilterConfiguration.class,
    DruidFilterConfiguration.class,
    WbWebStatFilterConfiguration.class})
public class WbAutoConfiguration {
	/**
	@Bean(initMethod = "init")
    @ConditionalOnMissingBean
    public DataSource dataSource() {
        return new DruidDataSourceWrapper();
    }**/
	@Bean
	@Primary
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(DataSourceProperties dataSourceProperties) {
		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
                tomcat.enableNaming();
                return super.getTomcatWebServer(tomcat);
            }
            @Override
            protected void postProcessContext(Context context) {
                try {
					File file = org.springframework.util.ResourceUtils.getFile("classpath:webapp");
					context.setDocBase(file.getAbsolutePath());
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
                ContextResource resource = new ContextResource();
                resource.setType("com.alibaba.druid.pool.DruidDataSource");
                String name = null == dataSourceProperties.getName()?"default":dataSourceProperties.getName();
                resource.setName(name);
                resource.setProperty("factory", "com.alibaba.druid.pool.DruidDataSourceFactory");
                resource.setProperty("name", name);
                String driverClassName = null == dataSourceProperties.getDriverClassName() ? dataSourceProperties.determineDriverClassName():dataSourceProperties.getDriverClassName();
                resource.setProperty("driverClassName", driverClassName);
                String url = null == dataSourceProperties.getUrl() ? dataSourceProperties.determineUrl():dataSourceProperties.getUrl();
                resource.setProperty("url", url);
                String username = null == dataSourceProperties.getUsername()?dataSourceProperties.determineUsername():dataSourceProperties.getUsername();
                resource.setProperty("username", username);
                String password = null == dataSourceProperties.getPassword() ? dataSourceProperties.determinePassword():dataSourceProperties.getPassword();
                resource.setProperty("password", password);
                resource.setProperty("filters", "stat,wall");
                		
                context.getNamingResources().addResource(resource);
                super.postProcessContext(context);
            }
        };
        tomcat.setPort(8888);
        return tomcat;
    }
	/**
	@Bean(initMethod = "init")
    @ConditionalOnMissingBean
    public DataSource dataSource(DataSourceProperties dataSourceProperties) throws IllegalArgumentException, NamingException {
        JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiName("java:comp/env/" + dataSourceProperties.getName());
        bean.setProxyInterface(DataSource.class);
        bean.setLookupOnStartup(false);
        bean.afterPropertiesSet();
        return (DataSource) bean.getObject();
    }
	@Bean(destroyMethod = "")
    public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
        JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiName("java:comp/env/" + databaseProperties().getJndiName());
        bean.setProxyInterface(DataSource.class);
        bean.setLookupOnStartup(false);
        bean.afterPropertiesSet();
        return (DataSource) bean.getObject();
    }**/
}
